home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 10 / AACD 10.iso / AACD / Online / SpeakFreely / src / gsm / add-test / add_test.c next >
C/C++ Source or Header  |  2000-05-18  |  5KB  |  257 lines

  1. /*
  2.  * Copyright 1992 by Jutta Degener and Carsten Bormann, Technische
  3.  * Universitaet Berlin.  See the accompanying file "COPYRIGHT" for
  4.  * details.  THERE IS ABSOLUTELY NO WARRANTY FOR THIS SOFTWARE.
  5.  */
  6.  
  7. /* $Header: /home/kbs/jutta/src/gsm/gsm-1.0/add-test/RCS/add_test.c,v 1.1 1992/10/28 00:09:10 jutta Exp $ */
  8.  
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <string.h>
  12.  
  13. #define    GSM_IMPLEMENTATION
  14. #include "gsm.h"
  15.  
  16. #include "../src/add.c"
  17.  
  18. int        interactive = 1;
  19.  
  20. char        * opname;
  21. longword    L_op1, L_op2, L_expect;
  22. word        op1, op2, expect;
  23. int        do_expect;
  24.  
  25. help()
  26. {
  27. puts( "  add a b      sub a b     mult a b   div    a b" );
  28. puts( "L_add A B    L_sub A B   L_mult A B   mult_r a b" );
  29. puts( "" );
  30. puts( "abs   a      norm  a        >> a b      << a b" );
  31. puts( "                          L_>> A B    L_<< A B" );
  32.  
  33. }
  34.  
  35. char * strtek P2((str, sep), char * str, char * sep) {
  36.  
  37.     static char     * S = (char *)0;
  38.     char        * c, * base;
  39.  
  40.     if (str) S = str;
  41.  
  42.     if (!S || !*S) return (char *)0;
  43.  
  44.     /*  Skip delimiters.
  45.      */
  46.     while (*S) {
  47.         for (c = sep; *c && *c != *S; c++) ;
  48.         if (*c) *S++ = 0;
  49.         else break;
  50.     }
  51.  
  52.     base = S;
  53.  
  54.     /*   Skip non-delimiters.
  55.      */
  56.     for (base = S; *S; S++) {
  57.  
  58.         for (c = sep; *c; c++)
  59.             if (*c == *S) {
  60.                 *S++ = 0;
  61.                 return base;
  62.             }
  63.     }
  64.  
  65.     return base == S ? (char *)0 : base;
  66. }
  67.  
  68. long value P1((s), char * s)
  69. {
  70.     switch (*s) {
  71.     case '-': switch (s[1]) {
  72.           case '\0': return MIN_WORD;
  73.           case '-':  return MIN_LONGWORD;
  74.           default:   break;
  75.           }
  76.           break;
  77.  
  78.     case '+': switch (s[1]) {
  79.           case '\0': return MAX_WORD;
  80.           case '+':  return MAX_LONGWORD;
  81.           default:   break;
  82.           }
  83.     default:  break;
  84.     }
  85.  
  86.     return strtol(s, (char **)0, 0);
  87. }
  88.  
  89. char * parse P1((buf), char * buf)
  90. {
  91.     char  * s, * a;
  92.     long    l;
  93.  
  94.     if (a = strchr(buf, '=')) *a++ = 0;
  95.  
  96.     opname = s = strtek(buf, " \t("); 
  97.     if (!s) return (char *)0;
  98.  
  99.     op1 = op2 = L_op1 = L_op2 = 0;
  100.  
  101.     if (s = strtek( (char *)0, "( \t,")) {
  102.         op1 = L_op1 = value(s);
  103.         if (s = strtek( (char *)0, ", \t)")) op2 = L_op2 = value(s);
  104.     }
  105.  
  106.     if (a) {
  107.         do_expect = 1;
  108.         while (*a == ' ' || *a == '\t') a++;
  109.         expect = L_expect = value(a);
  110.     }
  111.  
  112.     return opname;
  113. }
  114.  
  115. void fprint_word P2((f, w), FILE * f,  word w)
  116. {
  117.     if (!w) putc('0', f);
  118.     else fprintf(f, "0x%4.4x (%d%s)",
  119.         (unsigned int)w,
  120.         (int)w,
  121.         w == MIN_WORD? "/-" : (w == MAX_WORD ? "/+" : ""));
  122. }
  123.  
  124. void print_word P1((w), word w)
  125. {
  126.     fprint_word( stdout, w );
  127. }
  128.  
  129. void fprint_longword P2((f, w), FILE * f, longword w)
  130. {
  131.     if (!w) putc('0', f);
  132.     else fprintf(f, "0x%8.8x (%ld%s)",
  133.         w, w, w == MIN_WORD ? "/-"
  134.         : (w == MAX_WORD ? "/+"
  135.         : (w == MIN_LONGWORD ? "/--" 
  136.         : (w == MAX_LONGWORD ? "/++" : ""))));
  137. }
  138.  
  139. void print_longword P1((w),longword w)
  140. {
  141.     fprint_longword(stdout, w);
  142. }
  143.  
  144. void do_longword P1((w), longword w)
  145. {
  146.     if (interactive) print_longword(w);
  147.     if (do_expect) {
  148.         if (w != L_expect) {
  149.             if (!interactive) fprint_longword(stderr, w);
  150.             fprintf(stderr, " != %s (%ld, %ld) -- expected ",
  151.                 opname, L_op1, L_op2 );
  152.             fprint_longword(stderr, L_expect);
  153.             putc( '\n', stderr );
  154.         }
  155.     } else if (interactive) putchar('\n');
  156. }
  157.  
  158. void do_word P1((w), word w )
  159. {
  160.     if (interactive) print_word(w);
  161.     if (do_expect) {
  162.         if (w != expect) {
  163.             if (!interactive) fprint_word(stderr, w);
  164.             fprintf(stderr, " != %s (%ld, %ld) -- expected ",
  165.                 opname, L_op1, L_op2 );
  166.             fprint_word(stderr, expect);
  167.             putc('\n', stderr);
  168.         }
  169.     } else if (interactive) putchar('\n');
  170. }
  171.  
  172. int main()
  173. {
  174.     char    buf[299];
  175.     char    * c;
  176.  
  177.     interactive = isatty(fileno(stdin));
  178.  
  179.     for (;;) {
  180.         if (interactive) fprintf(stderr, "? ");
  181.  
  182.         if (!fgets(buf, sizeof(buf), stdin)) exit(0);
  183.         if (c = strchr(buf, '\n')) *c = 0;
  184.  
  185.         if (*buf == ';' || *buf == '#') continue;
  186.         if (*buf == '\'') {
  187.             puts(buf + 1);
  188.             continue;
  189.         }
  190.         if (*buf == '\"') {
  191.             fprintf(stderr,  "%s\n", buf + 1);
  192.             continue;
  193.         }
  194.  
  195.         c = parse(buf);
  196.  
  197.         if (!c) continue;
  198.         if (!strcmp(c,   "add")) {
  199.             do_word(    gsm_add( op1, op2 ));
  200.             continue;
  201.         }
  202.         if (!strcmp(c, "sub")) {
  203.             do_word(    gsm_sub( op1, op2 ));
  204.             continue;
  205.         }
  206.         if (!strcmp(c, "mult")) {
  207.             do_word(    gsm_mult( op1, op2 ));
  208.             continue;
  209.         }
  210.         if (!strcmp(c, "mult_r")) {
  211.             do_word(    gsm_mult_r(op1, op2));
  212.             continue;
  213.         }
  214.         if (!strcmp(c, "abs" )) {
  215.             do_word(    gsm_abs(op1) );
  216.             continue;
  217.         } 
  218.         if (!strcmp(c, "div" )) {
  219.             do_word(    gsm_div( op1, op2 ));
  220.             continue;
  221.         }
  222.         if (!strcmp(c,  "norm" )) {
  223.             do_word(    gsm_norm(L_op1));
  224.             continue;
  225.         } 
  226.         if (!strcmp(c,  "<<" )) {
  227.             do_word(    gsm_asl( op1, op2));
  228.             continue;
  229.         } 
  230.         if (!strcmp(c,  ">>" )) {
  231.             do_word(    gsm_asr( op1, op2 ));
  232.             continue;
  233.         }
  234.         if (!strcmp(c,  "L_mult")) {
  235.             do_longword( gsm_L_mult( op1, op2 ));
  236.             continue;
  237.         }
  238.         if (!strcmp(c,  "L_add" )) {
  239.             do_longword( gsm_L_add( L_op1, L_op2 ));
  240.             continue;
  241.         } 
  242.         if (!strcmp(c,  "L_sub" )) {
  243.             do_longword( gsm_L_sub( L_op1, L_op2 ));
  244.             continue;
  245.         } 
  246.         if (!strcmp(c,  "L_<<" )) {
  247.             do_longword(    gsm_L_asl( L_op1, L_op2 ));
  248.             continue;
  249.         } 
  250.         if (!strcmp(c,  "L_>>")) {
  251.             do_longword(    gsm_L_asr( L_op1, L_op2 ));
  252.             continue;
  253.         }
  254.         help();
  255.     }
  256. }
  257.